import {
  OrderModel
} from '../../models/orderData.js'
import {
  AuthModel
} from '../../models/authData.js'
let authModel = new AuthModel()
let orderModel = new OrderModel()
const { $Message } = require('../../components/base/index')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderDetail: '',
    companyInfo: '',
    ifAuthentication: false,
    orderStatus: {
      'Released': '已发布',
      'Underway': '执行中',
      'Accomplished': '已完成'
    },
    maintainType: {
      '1': '机电',
      '2': '钣金',
      '3': '喷漆'
    },
    attachments: [],
    FFixDes: '',
    starIndex: 0,
    visible: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: '订单明细'
    })
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#3B80FE',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
    wx.showLoading({
      mask: true
    })
    const detail = orderModel.getOrderDetail({
      entityId: options.FId
    })
    const auth = authModel.getSelfAuthData()
    const cInfo = authModel.getCompanyInfo({
      entityId: options.companyId
    })
    Promise.all([detail, auth, cInfo])
      .then(res => {
        this.setData({
          orderDetail: res[0].data.data.editJson.data,
          FFixDes: res[0].data.data.editJson.data.FFixDes ? res[0].data.data.editJson.data.FFixDes:'',
          ifAuthentication: res[1].data.data.resumeData.authenticationInfoList[0].FAuthenticationStatus == 'Audited' ? true : false,
          starIndex: res[0].data.data.editJson.data.FStarLevel ? res[0].data.data.editJson.data.FStarLevel:0,
          companyInfo: res[2].data.data.editJson.data,
          attachments: res[0].data.data.editJson.data.FAttachment.split('|')
        })
        wx.hideLoading()
      })
  },

  fixDesInput(e) {
    this.setData({
      FFixDes: e.detail.value
    })
  },

  //抢单
  robOrder() {
    if (!this.data.ifAuthentication) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '还未申请认证，请在"我"中申请认证'
      })
      return
    }
    const detail = orderModel.robOrder({
      args: {
        orderId: this.data.orderDetail.FId
      }
    })
    detail.then(res => {
      if (res.data.success) {
        wx.showModal({
          showCancel: false,
          title: '提示',
          content: '抢单成功',
          success(res) {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })
      } else {
        wx.showModal({
          showCancel: false,
          title: '提示',
          content: res.data.data.msg
        })
      }
    })
  },

  cencelOrder() {
    this.setData({
      visible: false
    })
    wx.navigateTo({
      url: `/pages/cancelOrder/cancelOrder?FId=${this.data.orderDetail.FId}&phone=${this.data.orderDetail.FMobile}`
    })
  },

  openModal() {
    this.setData({
      visible: true
    })
  },

  closeModal() {
    this.setData({
      visible: false
    })
  },

  completeOrder() {
    if(!this.data.FFixDes) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '请填写维修过程'
      })
      return
    }
    orderModel.saveOrder({
      args: {
        data: [{
          FId: this.data.orderDetail.FId,
          FFixDes: this.data.FFixDes
        }]
      }
    }).then(res => {
      return orderModel.completeOrder({
        args: {
          orderId: this.data.orderDetail.FId
        }
      })
    }).then(res => {
      if(res.data.success) {
        $Message({
          content: '提交成功',
          type: 'success',
          duration: 2
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 2000)
      }
    })
  },

  attachmentClick(e) {
    var urls = []
    var currentUrl
    for (var i = 0; i < this.data.attachments.length; i++) {
      var url = 'https://www.myhr100.com:4200/Ecp.Picture.view.img?pictureId=' + this.data.attachments[i]
      urls.push(url)
    }
    currentUrl = 'https://www.myhr100.com:4200/Ecp.Picture.view.img?pictureId=' + this.data.attachments[e.currentTarget.id]
    wx.previewImage({
      current: currentUrl,
      urls: urls,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})